package com.example.javafx.文件上传;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.util.List;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("File Chooser Sample");

        final FileChooser fileChooser = new FileChooser();
        final Button openButton = new Button("Open a picture");
        final Button openMultipButton = new Button("Open pictures...");

        openButton.setOnAction(event -> {
            File file = fileChooser.showOpenDialog(primaryStage);
            if(file != null){
                System.out.println(file.getName());
            }
        });

        openMultipButton.setOnAction(event -> {
            List<File> fileList = fileChooser.showOpenMultipleDialog(primaryStage);
            fileList.stream().map(File::getName).forEach(System.out::println);
        });

        final GridPane grid = new GridPane();
        GridPane.setConstraints(openButton,0,0);
        GridPane.setConstraints(openMultipButton,1,0);
        grid.setVgap(6);
        grid.setHgap(6);
        grid.getChildren().addAll(openButton,openMultipButton);

        final Group root = new Group();
        root.getChildren().add(grid);

        primaryStage.setScene(new Scene(root,300,250));
        primaryStage.show();;
    }
}
